package com.example.baomidou.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.baomidou.entity.SnowOperateLog;
import com.example.baomidou.mapper.SnowOperateLogMapper;
import com.example.baomidou.vo.OperateRecordReq;
import com.example.baomidou.vo.PageDto;
import com.example.baomidou.vo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Service
public class SnowOperateLogServiceImpl extends ServiceImpl<SnowOperateLogMapper, SnowOperateLog> implements
        ISnowOperateLogService {
 
    @Resource
    private SnowOperateLogMapper operateLogMapper;
 
    // 注意：不配置MybatisPlusConfig分页不生效  TokenKind
    @Override
    public Result<PageDto<SnowOperateLog>> queryList(OperateRecordReq request) {
        LambdaQueryWrapper<SnowOperateLog> wrapper = Wrappers.lambdaQuery(SnowOperateLog.class);
        IPage<SnowOperateLog> page = new Page<>(request.getPageNum(),request.getPageSize());
        wrapper
                .eq(SnowOperateLog::getOperator,request.getOperator())
                .eq(SnowOperateLog::getOperateType,request.getOperateType())
                .ge(SnowOperateLog::getOperateTime,request.getOperateTimeStart())
                .le(SnowOperateLog::getOperateTime,request.getOperateTimeEnd())
                .orderByDesc(SnowOperateLog::getId);
        IPage<SnowOperateLog> logIPage = operateLogMapper.selectPage(page,wrapper);
        PageDto<SnowOperateLog> result = new PageDto<>(logIPage.getTotal(), logIPage.getPages()
                , logIPage.getCurrent(), logIPage.getSize()
                , logIPage.getRecords());
        return Result.success(result);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<SnowOperateLog> selectAllByOperator(String operator) {
//        // 自定义方法和SQL实现功能
//        List<SnowOperateLog> snowOperateLogs = operateLogMapper.selectAllByOperator(operator);
        LambdaQueryWrapper<SnowOperateLog> queryWrapper = Wrappers.lambdaQuery(SnowOperateLog.class)
                .eq(SnowOperateLog::getOperator, operator);
        List<SnowOperateLog> snowOperateLogs = operateLogMapper.selectList(queryWrapper);
        // 批量打印日志
        snowOperateLogs.forEach(System.out::println);
        return snowOperateLogs;
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateByOperator(String operator) {
        LambdaUpdateWrapper<SnowOperateLog> wrapper = Wrappers.lambdaUpdate(SnowOperateLog.class)
                .eq(SnowOperateLog::getOperator, "张三")
                .set(SnowOperateLog::getOperator, "张三疯======");
        super.update(wrapper);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteById(String id) {
        LambdaQueryWrapper<SnowOperateLog> wrapper = Wrappers.lambdaQuery(SnowOperateLog.class)
                .eq(SnowOperateLog::getId, id);
        super.remove(wrapper);
    }
}